package com.softeem.xyblogmaster.controller;

import com.softeem.xyblogmaster.domain.Articles;
import com.softeem.xyblogmaster.domain.Tags;
import com.softeem.xyblogmaster.domain.Users;
import com.softeem.xyblogmaster.service.ArticlesService;
import com.softeem.xyblogmaster.service.AuthService;
import com.softeem.xyblogmaster.service.UsersService;
import com.softeem.xyblogmaster.util.AjaxResult;
import com.softeem.xyblogmaster.util.PageInfo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/users")
public class UsersController {


    //先注入Service
    @Autowired
    private UsersService usersService;

    //增加方法 http://localhost:8080/users/create
    @PostMapping("/create")
    public AjaxResult doAdd(@RequestBody @Valid Users users){
        usersService.save(users);
        return AjaxResult.ok("添加用户成功");
    }

    //http://localhost:8080/users/edit
    @PutMapping("/edit")
    public AjaxResult  doEdit(@RequestBody @Valid Users users){
        usersService.updateById(users);
        return AjaxResult.ok("修改用户成功");
    }

    //http://localhost:8080/users/remove/1
    @DeleteMapping("/remove/{uid}")
    public AjaxResult doRemove(@PathVariable("uid") int uid){
        usersService.removeById(uid);
        return AjaxResult.ok("删除用户成功");
    }

    //http://localhost:8080/users/detail/1
    @GetMapping("/detail/{uid}")
    public AjaxResult doDetail(@PathVariable("uid") @Min(1) int uid){
        Users byId = usersService.getById(uid);
        return AjaxResult.ok("查询用具成功",byId);

    }

//    //http://localhost:8080/list/1/10
//    @GetMapping("/list/{page}/{limit}")
//    public AjaxResult doList(@PathVariable("page") int page,
//                             @PathVariable("limit") int limit,
//                             @RequestParam("keyword") String keyword){
//
//        LambdaQueryWrapper<Articles> lambdaQueryWrapper=new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(Articles::getTitle,"%"+keyword+"%");
//        Page pages = articlesService.page(new Page<>(page,limit),lambdaQueryWrapper);
//        PageInfo pageInfo = new PageInfo(pages,7);
//        return AjaxResult.ok("查询成功",pageInfo);
//
//    }

    //http://localhost:8080/users?page=1&limit=10&keyword=java
    @GetMapping("/list")
    public AjaxResult doList(@RequestParam(value = "page",defaultValue = "1") int page,
                             @RequestParam(value = "limit",defaultValue = "10") int limit,
                             @RequestParam(value = "keyword",defaultValue = "") String keyword)

    {

        PageInfo pageInfo = usersService.page(page, limit, keyword);
        return AjaxResult.ok("查询用户成功",pageInfo);
    }

}
